package cn.com.guage.guc.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Resource {
	Lock lock = new ReentrantLock();

	public void printString(String Str, int num) {
		lock.lock();
		try {
			for (int i = 0; i < num; i++) {
				System.out.println(Thread.currentThread().getName() +"---------"+ i);
			}
		} catch (Exception e) {
			e.printStackTrace();
			lock.unlock();
		} finally {
			lock.unlock();
		}
	}
}

public class LockTest2 {

	public static void main(String[] args) {
		Resource rs = new Resource();
		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Thread threadA = new Thread(() -> {
				rs.printString("AAA", 2);

			}, "A");
			Thread threadB = new Thread(() -> {
				rs.printString("BBB", 4);
			}, "B");
			Thread threadC = new Thread(() -> {
				rs.printString("CCC", 6);
			}, "C");
			threadA.start();
			threadB.start();
			threadC.start();
		}
	}

}
